package cn.com.homeyoda.aimate.aihealth.ui.health_qa;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.databinding.library.baseAdapters.BR;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import java.util.ArrayList;
import java.util.List;

import cn.com.homeyoda.aimate.aihealth.R;
import cn.com.homeyoda.aimate.aihealth.app.AppViewModelFactory;
import cn.com.homeyoda.aimate.aihealth.base.BaseFragment;
import cn.com.homeyoda.aimate.aihealth.bean.MessageTypeListBean;
import cn.com.homeyoda.aimate.aihealth.bean.QuizListBean;
import cn.com.homeyoda.aimate.aihealth.databinding.FragmentHealthQaBinding;
import cn.com.homeyoda.aimate.aihealth.ui.health_qa.record.TestRecordActivity;
import cn.com.homeyoda.aimate.aihealth.ui.message.MessageTypeAdapter;

public class HealthQaListFragment extends BaseFragment<FragmentHealthQaBinding, HealthQaViewModel> {

    private MessageTypeAdapter messageTypeAdapter;
    private List<MessageTypeListBean.DataBean> typeList = new ArrayList<>();

    private HealthQaListAdapter healthQaListAdapter;
    private List<QuizListBean.RowsBean> healthQaListBeanList = new ArrayList<>();

    @Override
    public HealthQaViewModel initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(HealthQaViewModel.class);
    }

    @Override
    public void initData() {
        super.initData();
        viewModel.getTypeList(true);
        binding.tvTestRecords.setOnClickListener(v -> startActivity(new Intent(getActivity(), TestRecordActivity.class)));
        initRecyclerView();
    }

    private void initRecyclerView() {
        messageTypeAdapter = new MessageTypeAdapter(R.layout.message_title_item, typeList, getContext());
        binding.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));
        binding.setAdapter(messageTypeAdapter);

        messageTypeAdapter.setOnItemClickListener((adapter, view, position) -> {
            messageTypeAdapter.setPos(position);
            MessageTypeListBean.DataBean bean = (MessageTypeListBean.DataBean) adapter.getItem(position);
            if (bean != null) {
                viewModel.getQaListData(bean.getTypeValue());
            }
        });

        healthQaListAdapter = new HealthQaListAdapter(R.layout.health_qa_item, healthQaListBeanList, getContext());
        binding.setLayoutManager2(new GridLayoutManager(getContext(), 2));
        binding.setAdapter2(healthQaListAdapter);

        healthQaListAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            QuizListBean.RowsBean bean = healthQaListBeanList.get(position);
            HealthQaActivity.start(getActivity(), bean.getId(), bean.getExplanationType());
        });
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.typeListData.observe(this, typeBeans -> {
            typeList = typeBeans;
            messageTypeAdapter.setNewData(typeList);
        });

        viewModel.qaListData.observe(this, healthQaBeans -> {
            healthQaListBeanList = healthQaBeans;
            healthQaListAdapter.setNewData(healthQaListBeanList);
        });
    }

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_health_qa;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

}
